package org.jxkj.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ParseUtils {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    private static SimpleDateFormat sdfm = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    private static SimpleDateFormat sdfH = new SimpleDateFormat("yyyy/MM/dd HH");
    private static SimpleDateFormat sdfD = new SimpleDateFormat("yyyy/MM/dd");
    private static SimpleDateFormat sdfM = new SimpleDateFormat("yyyy/MM");
    private static SimpleDateFormat second = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat minute = new SimpleDateFormat("yyyyMMddHHmm");
    private static SimpleDateFormat hour = new SimpleDateFormat("yyyyMMddHH");
    private static SimpleDateFormat day = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat month = new SimpleDateFormat("yyyyMM");

    private static String STANDARD_FORMAT = "yyyy/MM/dd HH:mm:ss";

    /**
     * 将int类型的时间戳转换成date时间
     *
     * @param
     * @return
     */

    public static int bytes2Int(byte[] bytes )
    {
        int int1=bytes[0]&0xff;
        int int2=(bytes[1]&0xff)<<8;
        int int3=(bytes[2]&0xff)<<16;
        int int4=(bytes[3]&0xff)<<24;
        return int1|int2|int3|int4;
    }

    public static double bytes2Double(byte[] arr) {
        long value = 0;
        for (int i = 0; i < 8; i++) {
            value |= ((long) (arr[i] & 0xff)) << (8 * i);
        }
        return Double.longBitsToDouble(value);
    }

    public static String getDate(long timestamp) {
        return dateToStr(new Date(timestamp * 1000));
    }




    /**
     * 将时间转换成int类型的时间戳
     *
     * @param date
     * @return
     */
    public static int getTimestamp(String date) {
        Date time = strToDate(date);
        return (int) (time.getTime() / 1000);
    }


    /**
     * 解析时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static String parseTime(String time) throws Exception {
        String start = "";
        int length = time.length();
        //判断传过来的时间长度
        if (length == 19) {
            start = second.format(sdf.parse(time));
        } else if (length == 16) {
            start = minute.format(sdfm.parse(time));
        } else if (length == 13 || length == 12) { //时间参数到小时
            start = hour.format(sdfH.parse(time));
        } else if (length == 10) { //时间参数到天
            start = day.format(sdfD.parse(time));
        } else if (length == 7) { //时间参数到月
            start = month.format(sdfM.parse(time));
        } else {
            throw new TimeException("时间格式错误,输入格式：yyyy/MM/dd HH:mm:ss, yyyy/MM/dd HH:mm, yyyy/MM/dd HH, yyyy/MM/dd, yyyy/MM");
        }
        return start;
    }


    //str-Date
    //Date-str    
//将指定的字符串转化成指定格式的时间

    public static Date strToDate(String dateTimeStr, String formatStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    //将日期转化成指定格式的日期字符串
    public static String dateToStr(Date date, String formatStr) {
        if (date == null) {
            return " ";
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    //----------------重载上面的strToDate方法------------------
    public static Date strToDate(String dateTimeStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    //----------------重载上面的dateToStr方法------------------
    public static String dateToStr(Date date) {
        if (date == null) {
            return " ";
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }

}


class TimeException extends Exception{
    public TimeException(String message) {
        super(message);
    }
}
